<template>
    <section>
        <el-col :span="24">
            <el-form inline>
                <el-form-item>
                    <el-input size="small" v-model.trim="form.allocation_number" placeholder="单号, 多个请用“逗号”隔开" style="width: 250px;" clearable></el-input>
                </el-form-item>
                <el-form-item>
                    <el-select v-model="form.status" placeholder="审批状态" size="small" style="width: 110px" clearable>
                        <el-option :value=0 label="审核中"></el-option>
                        <el-option :value=1 label="已通过"></el-option>
                        <el-option :value=2 label="已拒绝"></el-option>
                        <el-option :value=3 label="已撤回"></el-option>
                        <el-option :value=4 label="已撤销"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-popover placement="bottom" width="450" trigger="click">
                        <el-form label-width="80px" label-position="left">
                            <el-form-item label="借出单号">
                                <el-input size="small" v-model.trim="form.source_allocation_number" placeholder="借出单号, 多个请用“逗号”隔开" style="width: 80%;" clearable></el-input>
                            </el-form-item>
                            <el-form-item label="申请事由">
                                <el-input size="small" v-model.trim="form.reason" placeholder="申请事由" style="width: 80%;" clearable></el-input>
                            </el-form-item>
                            <el-form-item label="发起时间">
                                <el-date-picker value-format="yyyy-MM-dd" v-model="form.createtime" type="daterange" size="small" style="width: 80%"
                                                range-separator="~" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
                            </el-form-item>
                        </el-form>
                        <el-button slot="reference" size="small" type="warning" plain>更多</el-button>
                    </el-popover>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" class="el-icon-search" @click="searchBtn" size="small" :loading="btnLoading">查询</el-button>
                </el-form-item>
                <el-form-item style="float: right">
                    <el-button size="small" class="el-icon-shopping-bag-1" type="primary" @click="saleVisible=true" plain>&nbsp;我要销售</el-button>
                </el-form-item>
            </el-form>
        </el-col>
        <el-col :span="24">
            <el-table :data="data" style="width: 100%" :max-height="tableHeight" v-loading="loading" size="small">
                <el-table-column type="expand">
                    <template slot-scope="{row}">
                        <el-table :data="row.detail" border size="mini" :header-cell-style="{ background: '#F3F4F7', color: '#555' }" style="width: 90%" max-height="400px">
                            <el-table-column v-for="(label, index) in row.detail_header" :key="index" :prop="label" :label="label" width="200">
                                <template slot-scope="{row}">
                                    <template v-if="row[label].length>1">
                                        <el-popover placement="bottom" width="200" trigger="hover">
                                            <el-col :span="24">
                                                <el-tag v-for="(ditem, dindex) in row[label]" :key="dindex" style="margin: 1px;" size="mini">
                                                    {{ditem.source_allocation_number}}：{{ditem.num}}
                                                </el-tag>
                                            </el-col>
                                            <el-tag slot="reference" size="mini">{{row[label][0].source_allocation_number}}：{{row[label][0].num}}</el-tag>
                                        </el-popover>
                                    </template>
                                    <el-tag v-else type="info" size="mini">{{row[label][0].source_allocation_number}}：{{row[label][0].num}}</el-tag>
                                </template>
                            </el-table-column>
                        </el-table>
                    </template>
                </el-table-column>
                <el-table-column prop="allocation_number" label="单号" width="170"></el-table-column>
                <el-table-column prop="approval_id" label="审批id" width="150" show-overflow-tooltip></el-table-column>
                <el-table-column prop="types" label="类型" width="90">
                    <template slot-scope="{ row }">
                        <el-tag size="mini">转销售</el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="adduser" label="发起人" width="80"></el-table-column>
                <el-table-column prop="createtime" label="发起时间" width="135"></el-table-column>
                <el-table-column prop="num" label="总数" width="100"></el-table-column>
                <el-table-column prop="reason" label="申请事由" show-overflow-tooltip></el-table-column>
                <el-table-column prop="status" label="审核状态">
                    <template slot-scope="{ row }">
                        <el-tag size="mini" v-if="row.status==0">审核中</el-tag>
                        <el-tag size="mini" v-else-if="row.status==1" type="success">已通过</el-tag>
                        <el-tag size="mini" v-else-if="row.status==2" type="danger">已拒绝</el-tag>
                        <el-tag size="mini" v-else-if="row.status==3" type="warning">已撤回</el-tag>
                        <el-tag size="mini" v-else-if="row.status==4" type="info">已撤销</el-tag>
                        <el-tag size="mini" v-else>{{ row.status }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="is_push" label="推送状态">
                    <template slot-scope="{ row }">
                        <el-tag size="mini" v-if="row.is_push==0" type="info">未推送</el-tag>
                        <el-tag size="mini" v-else-if="row.is_push==1" type="success">成功</el-tag>
                        <el-tag size="mini" v-else-if="row.is_push==2" type="danger">失败</el-tag>
                        <el-tag size="mini" v-else-if="row.is_push==3" type="warning">取消</el-tag>
                        <el-tag size="mini" v-else>{{ row.is_push }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" fixed="right" width="150">
                    <template scope="{ row }">
                        <el-button class="el-icon-document" type="text" size="small" @click="detailBtn(row.allocation_number)">&nbsp;详情</el-button>
                        <el-button class="el-icon-share" type="text" size="small" @click="processBtn(row)"  :disabled="!(row.approval_id)">&nbsp;审批进度</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!--翻页栏-->
            <div style="margin-top: 4px;overflow: hidden;float: right;">
                <Page :total="total" :current="page" :page-size="limit" show-total @on-change="pageChange"></Page>
            </div>
        </el-col>

        <!--借转销售详情-->
        <el-drawer title="借转销售详情" :visible.sync="detailVisible" size="1200px" direction="rtl" :wrapperClosable="false">
            <el-row style="padding: 0 20px;">
                <div>
                    <svg t="1717659283335" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="9445" width="16" height="16"><path d="M407.466667 477.866667h-277.333334C93.866667 477.866667 64 448 64 409.6v-277.333333c0-36.266667 29.866667-66.133333 66.133333-66.133334h277.333334c36.266667 0 66.133333 29.866667 66.133333 66.133334v277.333333c2.133333 38.4-27.733333 68.266667-66.133333 68.266667zM132.266667 108.8c-12.8 0-25.6 10.666667-25.6 25.6v277.333333c0 12.8 10.666667 25.6 25.6 25.6h277.333333c12.8 0 25.6-10.666667 25.6-25.6v-277.333333c0-12.8-10.666667-25.6-25.6-25.6h-277.333333zM407.466667 960h-277.333334C93.866667 960 64 930.133333 64 893.866667v-277.333334c0-36.266667 29.866667-66.133333 66.133333-66.133333h277.333334c36.266667 0 66.133333 29.866667 66.133333 66.133333v277.333334c2.133333 36.266667-27.733333 66.133333-66.133333 66.133333zM132.266667 590.933333c-12.8 0-25.6 10.666667-25.6 25.6v277.333334c0 12.8 10.666667 25.6 25.6 25.6h277.333333c12.8 0 25.6-10.666667 25.6-25.6v-277.333334c0-12.8-10.666667-25.6-25.6-25.6h-277.333333z m618.666666-110.933333c-17.066667 0-34.133333-6.4-46.933333-19.2L563.2 320c-12.8-12.8-19.2-29.866667-19.2-46.933333s6.4-34.133333 19.2-46.933334L704 83.2c12.8-12.8 29.866667-19.2 46.933333-19.2 17.066667 0 34.133333 6.4 46.933334 19.2l140.8 140.8c12.8 12.8 19.2 29.866667 19.2 46.933333 0 17.066667-6.4 34.133333-19.2 46.933334l-138.666667 142.933333c-12.8 12.8-29.866667 19.2-49.066667 19.2z m0-373.333333c-6.4 0-12.8 2.133333-17.066666 6.4l-140.8 140.8c-4.266667 4.266667-6.4 10.666667-6.4 17.066666 0 6.4 2.133333 12.8 6.4 17.066667l140.8 140.8c4.266667 4.266667 10.666667 6.4 17.066666 6.4 6.4 0 12.8-2.133333 17.066667-6.4l140.8-140.8c4.266667-4.266667 6.4-10.666667 6.4-17.066667 0-6.4-2.133333-12.8-6.4-17.066666L768 113.066667c-4.266667-4.266667-8.533333-6.4-17.066667-6.4z m138.666667 853.333333h-277.333333c-36.266667 0-66.133333-29.866667-66.133334-66.133333v-277.333334c0-36.266667 29.866667-66.133333 66.133334-66.133333h277.333333c36.266667 0 66.133333 29.866667 66.133333 66.133333v277.333334c2.133333 36.266667-27.733333 66.133333-66.133333 66.133333zM614.4 590.933333c-12.8 0-25.6 10.666667-25.6 25.6v277.333334c0 12.8 10.666667 25.6 25.6 25.6h277.333333c12.8 0 25.6-10.666667 25.6-25.6v-277.333334c0-12.8-10.666667-25.6-25.6-25.6h-277.333333z" fill="#1E87EB" p-id="9446"></path></svg>
                    <span style="font-size: 16px;font-weight: bold;vertical-align: bottom;margin-left: 4px;">商品信息：{{detail.allocation_number}}</span>
                </div>
                <el-row style="width: 100%;border-top: 1px solid #ebeef5;border-left: 1px solid #ebeef5;border-right: 1px solid #ebeef5;">
                    <el-row style="height: 40px;padding: 10px;background-color: #f5f7fa;">
                        <span style="font-weight: bold;color: #909399">申请计划列表</span>
                        <span style="float: right">
                            <span style="margin-right: 20px;color: #909399">发起人：{{detail.adduser}}&nbsp;&nbsp;{{detail.createtime}}</span>
                            总件数：<span style="font-weight: bold;font-size: 14px;margin-right:10px;color: #409eff;">{{detail.num}}</span>
                            <el-tag size="mini" v-if="detail.status==0">审核中</el-tag>
                            <el-tag size="mini" v-else-if="detail.status==1" type="success">已通过</el-tag>
                            <el-tag size="mini" v-else-if="detail.status==2" type="danger">已拒绝</el-tag>
                            <el-tag size="mini" v-else-if="detail.status==3" type="warning">已撤回</el-tag>
                            <el-tag size="mini" v-else-if="detail.status==4" type="warning">已撤销</el-tag>
                            <el-tag size="mini" v-else>{{ detail.status }}</el-tag>
                        </span>
                    </el-row>
                    <el-table v-if="detail.list" :data="detail.list" style="width: 100%" :max-height="drawerTableHeight" size="mini">
                        <el-table-column prop="img" label="图片" width="50">
                            <template slot-scope="{row}">
                                <el-image :src="row.img" fit="contian" :preview-src-list="[row.img]">
                                    <div slot="error" class="image-slot">
                                        <svg t="1718681716906" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2623" width="20" height="20"><path d="M400.696 268.795c-17.249 0-31.233 13.986-31.233 31.233v30.471c0 17.249 13.986 31.233 31.233 31.233s31.233-13.986 31.233-31.233v-30.471c0-17.249-13.985-31.233-31.233-31.233z" fill="#999999" p-id="2624"></path><path d="M623.649 361.734c17.249 0 31.234-13.986 31.234-31.233v-30.471c0-17.249-13.986-31.233-31.234-31.233s-31.233 13.986-31.233 31.233v30.471c-0.001 17.248 13.985 31.233 31.233 31.233z" fill="#999999" p-id="2625"></path><path d="M438.295 388.804c-14.656 9.104-19.155 28.362-10.050 43.013 11.209 18.047 41.976 48.59 86.157 48.59 43.958 0 75.1-30.313 86.574-48.223 9.303-14.529 5.068-33.847-9.455-43.15-14.539-9.298-33.852-5.068-43.15 9.455-0.122 0.199-13.38 19.45-33.969 19.45-20.009 0-32.444-18.128-33.278-19.373-9.166-14.423-28.28-18.805-42.829-9.761z" fill="#999999" p-id="2626"></path><path d="M824.508503 116.690676 571.592236 116.690676c-17.248849 0-31.233352 13.985526-31.233352 31.233352s13.985526 31.233352 31.233352 31.233352l252.916267 0c40.181141 0 72.878844 32.692586 72.878844 72.878844l0 396.966057-189.334159-165.29465c-12.20088-10.655687-30.517037-10.207479-42.173518 0.9967L468.578048 674.16231 309.521472 517.519714c-11.895935-11.70253-30.903847-12.002358-43.154869-0.645706L126.957507 646.163629l0-394.126382c0-40.186258 32.692586-72.878844 72.878844-72.878844l252.916267 0c17.248849 0 31.233352-13.985526 31.233352-31.233352S470.000444 116.690676 452.751594 116.690676L199.836351 116.690676c-74.632791 0-135.346571 60.71378-135.346571 135.346571l0 520.56405c0 74.632791 60.71378 135.346571 135.346571 135.346571l252.916267 0c17.248849 0 31.233352-13.985526 31.233352-31.233352s-13.985526-31.233352-31.233352-31.233352L199.836351 845.481164c-40.186258 0-72.878844-32.692586-72.878844-72.878844l0-41.23924 160.003134-148.385539 159.428036 157.007917c12.048407 11.865235 31.361265 11.981892 43.546795 0.274246l198.576661-190.68697 208.876238 182.346001 0 40.683585c0 40.186258-32.697703 72.878844-72.878844 72.878844L571.592236 845.481164c-17.248849 0-31.233352 13.985526-31.233352 31.233352s13.985526 31.233352 31.233352 31.233352l252.916267 0c74.627674 0 135.346571-60.71378 135.346571-135.346571L959.855074 252.037247C959.855074 177.404456 899.136178 116.690676 824.508503 116.690676z" fill="#999999" p-id="2627"></path></svg>
                                    </div>
                                </el-image>
                            </template>
                        </el-table-column>
                        <el-table-column prop="source_allocation_number" label="借出单号" width="170"></el-table-column>
                        <el-table-column prop="type_name" label="款名" show-overflow-tooltip></el-table-column>
                        <el-table-column prop="sku_code" label="规格编码" width="130"></el-table-column>
                        <el-table-column prop="num" label="计划/实际借出数" width="120">
                            <template slot-scope="{row}">
                                <span style="font-size: 16px;font-weight: bold">{{row.num}}</span> /
                                <span style="font-size: 16px;font-weight: bold;color: #ed4014">{{row.actually_num}}</span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="actually_return_num" label="已归还数" width="80">
                            <template slot-scope="{row}">
                                <span style="font-size: 16px;font-weight: bold" :style="{color: row.actually_return_num==0?'#dcdee2':''}">{{row.actually_return_num}}</span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="ipurchase_num" label="已转内购数" width="80">
                            <template slot-scope="{row}">
                                <span style="font-size: 16px;font-weight: bold" :style="{color: row.ipurchase_num==0?'#dcdee2':''}">{{row.ipurchase_num}}</span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="give_num" label="已转赠送数" width="80">
                            <template slot-scope="{row}">
                                <span style="font-size: 16px;font-weight: bold" :style="{color: row.give_num==0?'#dcdee2':''}">{{row.give_num}}</span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="sale_num" label="已转销售数" width="80">
                            <template slot-scope="{row}">
                                <span style="font-size: 16px;font-weight: bold" :style="{color: row.sale_num==0?'#dcdee2':''}">{{row.sale_num}}</span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="sale_num" label="本次计划数" width="80">
                            <template slot-scope="{row}">
                                <span style="font-size: 16px;font-weight: bold" :style="{color: row.plan_num==0?'#dcdee2':''}">{{row.plan_num}}</span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="price" label="结算金额" width="80">
                            <template slot-scope="{row}">
                                <span style="font-size: 16px;font-weight: bold">{{row.price}}</span>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-row>
                <div style="margin-top: 10px;">
                    <svg t="1717659283335" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="9445" width="16" height="16"><path d="M407.466667 477.866667h-277.333334C93.866667 477.866667 64 448 64 409.6v-277.333333c0-36.266667 29.866667-66.133333 66.133333-66.133334h277.333334c36.266667 0 66.133333 29.866667 66.133333 66.133334v277.333333c2.133333 38.4-27.733333 68.266667-66.133333 68.266667zM132.266667 108.8c-12.8 0-25.6 10.666667-25.6 25.6v277.333333c0 12.8 10.666667 25.6 25.6 25.6h277.333333c12.8 0 25.6-10.666667 25.6-25.6v-277.333333c0-12.8-10.666667-25.6-25.6-25.6h-277.333333zM407.466667 960h-277.333334C93.866667 960 64 930.133333 64 893.866667v-277.333334c0-36.266667 29.866667-66.133333 66.133333-66.133333h277.333334c36.266667 0 66.133333 29.866667 66.133333 66.133333v277.333334c2.133333 36.266667-27.733333 66.133333-66.133333 66.133333zM132.266667 590.933333c-12.8 0-25.6 10.666667-25.6 25.6v277.333334c0 12.8 10.666667 25.6 25.6 25.6h277.333333c12.8 0 25.6-10.666667 25.6-25.6v-277.333334c0-12.8-10.666667-25.6-25.6-25.6h-277.333333z m618.666666-110.933333c-17.066667 0-34.133333-6.4-46.933333-19.2L563.2 320c-12.8-12.8-19.2-29.866667-19.2-46.933333s6.4-34.133333 19.2-46.933334L704 83.2c12.8-12.8 29.866667-19.2 46.933333-19.2 17.066667 0 34.133333 6.4 46.933334 19.2l140.8 140.8c12.8 12.8 19.2 29.866667 19.2 46.933333 0 17.066667-6.4 34.133333-19.2 46.933334l-138.666667 142.933333c-12.8 12.8-29.866667 19.2-49.066667 19.2z m0-373.333333c-6.4 0-12.8 2.133333-17.066666 6.4l-140.8 140.8c-4.266667 4.266667-6.4 10.666667-6.4 17.066666 0 6.4 2.133333 12.8 6.4 17.066667l140.8 140.8c4.266667 4.266667 10.666667 6.4 17.066666 6.4 6.4 0 12.8-2.133333 17.066667-6.4l140.8-140.8c4.266667-4.266667 6.4-10.666667 6.4-17.066667 0-6.4-2.133333-12.8-6.4-17.066666L768 113.066667c-4.266667-4.266667-8.533333-6.4-17.066667-6.4z m138.666667 853.333333h-277.333333c-36.266667 0-66.133333-29.866667-66.133334-66.133333v-277.333334c0-36.266667 29.866667-66.133333 66.133334-66.133333h277.333333c36.266667 0 66.133333 29.866667 66.133333 66.133333v277.333334c2.133333 36.266667-27.733333 66.133333-66.133333 66.133333zM614.4 590.933333c-12.8 0-25.6 10.666667-25.6 25.6v277.333334c0 12.8 10.666667 25.6 25.6 25.6h277.333333c12.8 0 25.6-10.666667 25.6-25.6v-277.333334c0-12.8-10.666667-25.6-25.6-25.6h-277.333333z" fill="#1E87EB" p-id="9446"></path></svg>
                    <span style="font-size: 16px;font-weight: bold;vertical-align: bottom;margin-left: 4px;">申请事由</span>
                </div>
                <el-input autosize readonly type="textarea" :value="detail.reason" style="width: 500px"></el-input>
            </el-row>
        </el-drawer>

        <!--我要销售-->
        <el-drawer title="我要销售" :visible.sync="saleVisible" size="1200px" direction="rtl" :wrapperClosable="false">
            <my-sale v-if="saleVisible" @close-hander="saleVisible=false" @data-hander="getData"></my-sale>
        </el-drawer>

        <!--审核进度-->
        <el-drawer title="调拨审批进度" :visible.sync="approvalVisible" size="650px" direction="rtl" :wrapperClosable="false" :before-close="closeBtn">
            <process-allocation v-if="approvalVisible" :runId="runId" :id="saleId" :type="'JZX'"></process-allocation>
        </el-drawer>
    </section>
</template>

<script>
import {lendsaleDetail, lendsaleMyList} from "@/api/op/lendsale";
import mySale from "@/view/op/components/mySale";
import processAllocation from "@/view/components/flow/processAllocation";

export default {
    name: "op_lendsale",
    components:{mySale, processAllocation},
    data() {
        return {
            tableHeight: window.innerHeight - 210,
            selectIndex: 0,
            btnLoading: false,
            loading: false,
            page: 1,
            limit: 20,
            total: 0,
            data: [],
            user: {},
            nodeActions: [],

            form: {allocation_number: "", status: "",source_allocation_number: "", reason: "",createtime: []},

            drawerTableHeight:window.innerHeight - 260+"px",
            detailVisible: false,
            detail: {},

            saleVisible: false,

            //审批流程
            approvalVisible: false,
            runId: "",
            saleId: "",
        }
    },
    created() {
        this.user = this.$store.getters.user;
        this.nodeActions = this.user.node_actions[this.$route.meta.id] ? this.user.node_actions[this.$route.meta.id] : [];
    },
    methods: {
        //搜索按钮
        searchBtn() {
            this.page = 1;
            this.getData();
        },
        //翻页
        pageChange(page) {
            this.page = page;
            this.getData();
        },
        //获取数据
        getData() {
            let param = {
                page: this.page,
                limit: this.limit
            };
            param = Object.assign(this.form, param);
            this.loading = true;
            lendsaleMyList(param).then((res) => {
                this.loading = false;
                this.total = res.data.total;
                this.data = res.data.data;
            }).catch(() => {
                this.loading = false;
            });
        },
        detailBtn(allocationNumber){
            this.loading = true;
            lendsaleDetail({allocation_number: allocationNumber}).then((res) =>{
                this.loading = false;
                this.detail = res.data;
                this.detailVisible = true;
            }).catch(()=>{
                this.loading = false;
            })
        },
        closeBtn(){
            this.approvalVisible = false;
        },
        processBtn(row){
            //审批流程
            this.approvalVisible = true;
            this.runId = row.approval_id;
            this.saleId = row.id;
        },
    },
    mounted() {
        this.searchBtn();
    }
}
</script>

<style scoped>

</style>
